<template>
  <div :class="{ 'empty-back-loading-page': true, 'empty-back-choose-page': showTenant }"></div>
</template>
<script setup lang="ts">
import {
  ref,
  reactive,
  toRefs,
  watch,
  onMounted,
  getCurrentInstance
} from 'vue'
import type { ComponentInternalInstance } from 'vue'
import { useRouter, useRoute } from "vue-router"
import useUserInfoStore from '@/store/userInfo';
// import bulletinDialog from '@/utils/bulletin.js'
const instance: any = getCurrentInstance() as ComponentInternalInstance;
const proxy: any = instance.proxy as any; // 访问 proxy 属性
const router = useRouter();
const route = useRoute();
const userStore = useUserInfoStore()
const emit = defineEmits([""])
const props = defineProps({

})
const showTenant = ref(false)
const code = ref('')
const showType = ref('')

onMounted(() => {
  if (route.query) {
    showType.value = 'login'
    let query = route.query
    userStore.LoginByUsername(Object.assign({ code: query.code, loginType: 'DINGTALK_SCAN' }, query)).then((data) => {
      localStorage.setItem('loginRandom', '')
      getTenantByUserList(data)
    }).catch((err) => {
      router.push({ path: '/login' })
    })
  }
})
// 获取该用户下所有的租户列表
function getTenantByUserList(data) {
  let list = []
  if (data.userDto && data.userDto.tenants) {
    list = data.userDto.tenants
    if (list.length > 0) {
      // 只有一个租户 或指定租户 直接进去
      if (list.length == 1 || (data.userDto.tenantId && data.userDto.tenant)) {
        userStore.setSwitchTenant(false)
        setUserInfoData(data)
      } else {
        userStore.setSwitchTenant(true)
        switchTenant(list)
      }
    }
  }
}
// 加入成功后写入数据
function setUserInfoData(data) {
  if (data) {
    userStore.batchSetStore(data)
    userStore.setTenantId(data.userDto.tenantId)
    userStore.setTenantInfo(data.userDto.tenant)
    router.push({ path: '/index' });
  }
}
// 选择租户登录
function switchTenant(list) {
  let obj = {
    successClose: false,
    switchTenant: true,
    switchList: list,
    afterLogin: (dialog, res) => {
      // bulletinDialog.install()
      dialog.handleClose()
      router.push({ path: '/index' });
    }
  }
  showTenant.value = true
  proxy.$loginForm(obj)
}
</script>
<style lang="scss" scoped>
.empty-back-loading-page {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  box-sizing: border-box;
  background-color: rgba(255, 255, 255, 0.8);
  background-image: url('../../../../public/jf-ui-public/img/loading.gif');
  background-repeat: no-repeat;
  background-position: center;
  background-position: center;
  background-size: 200px 160px;
}

.empty-back-choose-page {
  background: rgba(0, 0, 0, .3);
}
</style>
